package org.spring.springboot.utility;

import org.spring.springboot.response.BaseResponse;
import org.spring.springboot.response.ImageReturnRes;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

/**
 * Time: 2017/11/14 11:32.
 * Created: vista @ ebudaowei.cn
 */
public class Utils {

    public static boolean downloadRequestFile(MultipartFile file, String path, BaseResponse baseResponse)
    {
        String fileName = file.getOriginalFilename();// 文件原名称
        // 判断文件类型
        String type = fileName.contains(".")?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;


        if (type != null) {// 判断文件类型是否为空
            if ("PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {

                try {
                    file.transferTo(new File(path));
                    return true;
                }catch (IOException ioe){
                    baseResponse.setMessage("save file("+path+") failed");
                }
            }else {
                baseResponse.setMessage("error file("+path+") type");
            }
        }else {
            baseResponse.setMessage("undefined file("+path+") type");
        }
        return false;
    }
}
